<?php
    class Pager_Simple extends Pager
    {
		private $__CurrentPage=NULL;
		private $__RowsCount=NULL;
		private $__PageCount=NULL;
		private $__Parameters=NULL;
		private $__Template=NULL;
		
        public function Build(&$data,$params=NULL)
        {
            $attr=($data['Meta']==NULL ? $data:$data['Meta']);
			$this->__CurrentPage=$attr['CurrentPage'];
			$this->__RowsCount=$attr['RowCount'];
			$this->__PageCount=$attr['PageCount'];

            //分页逻辑
			$pageText='';
            if($this->__RowsCount>0)
			{
				//绑定模板
				if(@$params['CurtomTags']){
					$this->__Template=$params['CurtomTags'];
				}
				else{
					$this->__Template=$this->GetTemplate();
				}
				
				//绑定参数
				$this->__Parameters='';
				{
                	if(@$params['Parameters']){
						$this->__Parameters=Functions::ParseArrayToQuerystring($params['Parameters']);
					}
				}
                $this->__Parameters=($this->__Parameters=='' ? '':'&'.$this->__Parameters);
				
				//当前页是第一页的时候，总页数大于1
                if($this->__CurrentPage==1&&$this->__PageCount>1)
				{
					$pageText='<span style="color:gray;">'.$this->__Template['first'].'</span> ';
					$pageText=$pageText.'<span style="color:gray;">'.$this->__Template['pre'].'</span> ';
					$pageText=$pageText.$this->Dispose_DetailMiddle();
					$pageText=$pageText.'<a href="?page='.($this->__CurrentPage+1).$this->__Parameters.'">'.$this->__Template['next'].'</a> ';
					$pageText=$pageText.'<a href="?page='.$this->__PageCount.$this->__Parameters.'">'.$this->__Template['last'].'</a> ';
					$pageText=$pageText.$this->Dispose_DetailLast();
				}
				//当前页是最后一页的时候，总页数大于1
                else if($this->__CurrentPage==$this->__PageCount&&$this->__PageCount>1)
				{
					$pageText='<a href="?page=1'.$this->__Parameters.'">'.$this->__Template['first'].'</a> ';
					$pageText=$pageText.'<a href="?page='.($this->__CurrentPage-1).$this->__Parameters.'">'.$this->__Template['pre'].'</a> ';
					$pageText=$pageText.$this->Dispose_DetailMiddle();					
					$pageText=$pageText.'<span style="color:gray;">'.$this->__Template['next'].'</span> ';
					$pageText=$pageText.'<span style="color:gray;">'.$this->__Template['last'].'</span>';
					$pageText=$pageText.$this->Dispose_DetailLast();				
				}
				//当前页是最后一页的时候，总页数等于1
                else if($this->__CurrentPage==$this->__PageCount&&$this->__PageCount==1)
				{
					$pageText='<span style="color:gray;">'.$this->__Template['first'].'</span>';
					$pageText=$pageText.'<span style="color:gray;">'.$this->__Template['pre'].'</span>';
					$pageText=$pageText.$this->Dispose_DetailMiddle();					  
					$pageText=$pageText.'<span style="color:gray;">'.$this->__Template['next'].'</span>';
					$pageText=$pageText.'<span style="color:gray;">'.$this->__Template['last'].'</span>';
					$pageText=$pageText.$this->Dispose_DetailLast();					  
				}
				//中间页时候
                else
				{
					$pageText='<a href="?page=1'.$this->__Parameters.'">'.$this->__Template['first'].'</a> ';
					$pageText=$pageText.'<a href="?page='.($this->__CurrentPage-1).$this->__Parameters.'">'.$this->__Template['pre'].'</a> ';
					$pageText=$pageText.$this->Dispose_DetailMiddle();
					$pageText=$pageText.'<a href="?page='.($this->__CurrentPage+1).$this->__Parameters.'">'.$this->__Template['next'].'</a> ';
					$pageText=$pageText.'<a href="?page='.$this->__PageCount.$this->__Parameters.'">'.$this->__Template['last'].'</a>';
					$pageText=$pageText.$this->Dispose_DetailLast();				
				}
            }
			
			//返回页码
			return $pageText;
        }	

		//处理中间段跳转		
		private function Dispose_DetailMiddle()
		{
			$tmp=str_replace('pageCode',$this->__CurrentPage,$this->__Template['detail_middle']);
			$tmp=str_replace('pageCount',$this->__PageCount,$tmp);						
			$tmp=str_replace('pageLink','?page=\'+document.getElementById(\'PHP_PagerSimple_ControlValue\').value+\''.$this->__Parameters,$tmp);	
			return $tmp;		
		}
		
		//处理尾段跳转
		private function Dispose_DetailLast()
		{
			return str_replace('rowsCount',$this->__RowsCount,$this->__Template['detail_last']);
		}
		
		private function GetTemplate()
		{
			$tmp=array('pre'=>'<<','next'=>'>>','first'=>'<','last'=>'>');
			$tmp['detail_middle']='第';
			{
				$tmp['detail_middle'].='<input type="text" ';
				$tmp['detail_middle'].='id="PHP_PagerSimple_ControlValue" ';
				$tmp['detail_middle'].='name="PHP_PagerSimple_ControlValue" ';
				$tmp['detail_middle'].='style=" font-size:10px;margin:0px 3px 3px 3px;padding:0px;width:20px;height:11px;" ';
				$tmp['detail_middle'].=' />';
			}
			$tmp['detail_middle'].='页';			
			$tmp['detail_middle'].=' 共<span style="padding:0px 2px;">pageCount</span>页';
			$tmp['detail_middle'].='<span style="cursor:pointer;" onclick="window.location=\'pageLink\';"> 跳转 </span>';
			 
			$tmp['detail_last']=' 共<span style="padding:0px 2px;">rowsCount</span>条记录 ';
			return $tmp;
		}
    }
?>



























